package com.briup.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.util.Result;
import com.briup.system.pojo.SysRole;
import com.briup.system.pojo.SysUser;
import com.briup.system.service.ISysRoleService;
import com.google.inject.internal.cglib.proxy.$LazyLoader;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author briup
 * @since 2022-11-15
 */
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @PostMapping("/saveOrUpdate")
    public Result<String> saveOrUpdate(SysRole role){
        boolean b = roleService.saveOrUpdate(role);
        return commonBoolean(b,"操作成功",1);
    }
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        boolean b = roleService.removeById(id);
        return commonBoolean(b,"单个删除成功",1);
    }
    @DeleteMapping("/deleteBatch")
    public Result<String> delete(List<Long> ids){
        boolean b = roleService.removeBatchByIds(ids);
        return commonBoolean(b,"批量删除成功",ids.size());
    }

    @ApiOperation(value = "角色查询",notes = "集分页查询和角色名字模糊查询为一体的接口")
    @PostMapping("/selectAllByPage")
    public Result<Page<SysRole>> selectAllByPage(@RequestBody Map<String,String> map){
        // 根据map提取信息
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        String roleName = map.get("roleName");
        // 角色部分的模糊查询，不是很必要，但是可以提供
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(roleName)){
            wrapper.like("role_name","%"+roleName+"%");
        }
        Page<SysRole> page = roleService.page(new Page<>(pageNum,pageSize),wrapper);
        return Result.success("角色查询成功",page, page.getRecords().size());
    }




    /**
     * 把增删改返回的内容判断封装成一个方法，不用每次都写那么多
     * @param msg
     * @param count
     * @param <T>
     * @return
     */
    private <T> Result<T> commonBoolean(boolean b,String msg,int count){
        if(b){
            return Result.success(msg,null,count);
        }else {
            return Result.fail();
        }
    }

}
